//
//  PlacesMapViewController.m
//  TopPlaces
//
//  Created by janakiraman gopinath on 12/12/12.
//  Copyright (c) 2012 GOPI. All rights reserved.
//

#import "MapViewController.h"

@interface MapViewController() 


@end

@implementation MapViewController
@synthesize annotations = _annotations;
@synthesize mapView = _mapView;
@synthesize accessoryEnabled  = _accessoryEnabled;

- (IBAction)changeMapType:(UISegmentedControl *)sender {
    switch (sender.selectedSegmentIndex) {
        case 0: {
            self.mapView.mapType = MKMapTypeStandard;
            break;
        } 
        case 1: {
            self.mapView.mapType = MKMapTypeSatellite;
            break;
        } 
        default: {
            self.mapView.mapType = MKMapTypeHybrid;
            break;
        } 
    }
}

- (void)updateMapView {
    if (self.mapView.annotations) [self.mapView removeAnnotations:self.mapView.annotations];
    if (self.annotations) {
        [self.mapView addAnnotations:self.annotations];
        MKMapRect zoomRect = MKMapRectNull;
        for (id <MKAnnotation> annotation in self.mapView.annotations) {
            MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
            MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
            if (MKMapRectIsNull(zoomRect)) {
                zoomRect = pointRect;
            } else {
                zoomRect = MKMapRectUnion(zoomRect, pointRect);
            }
        }
        [self.mapView setVisibleMapRect:zoomRect animated:YES];
    }
}

- (void)setMapView:(MKMapView *)mapView {
    _mapView = mapView;
    [self updateMapView];
}

- (void)setAnnotations:(NSArray *)annotations {
   // NSLog(@"Annotations set");
    _annotations = annotations;
    [self updateMapView];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.mapView.delegate = self;
}

- (void)viewDidUnload {
    [self setMapView:nil];
    [super viewDidUnload];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return YES;
}


@end
